package com.vast.ui.ledpanel.views;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewSite;

import socketinterface.CommunicationManager;
import socketinterface.ServerController;

import com.vast.ui.component.models.Led;
import com.vast.ui.component.models.LedPanel;
import com.vast.ui.component.providers.LedContentProvider;
import com.vast.ui.component.providers.LedEditingSupport;
import com.vast.ui.component.providers.LedLabelProvider;
import com.vast.ui.ledpanel.Activator;

public class ViewPart extends org.eclipse.ui.part.ViewPart {
	public Display    display;
	public Image      image;
	public LedPanel   ledPanel;
	public TableViewer viewer;
	//ServerController serverController;
	public CommunicationManager communicationManager;
	
	public void startServerController() {
		//serverController = new ServerController(this);
		//serverController.start();
		communicationManager.startServer();
	}
	
	public ViewPart() {
		// TODO Auto-generated constructor stub	
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		display = parent.getDisplay();
		viewer = createTableViewer(parent, 8);
		
		ledPanel = LedPanel.getInstance();
		initLedPanel(viewer, ledPanel, 4, 0);
		communicationManager = new CommunicationManager(this, 20000);
	}

	public void initLedPanel(final TableViewer viewer, final LedPanel ledPanel, int numberOfRaw, int itnitialValue) {
		// Create initial data of MemoryImage
		ledPanel.getList().clear();
		
		for(int i = 0; i < numberOfRaw; i++) {
			Led led = new Led(i, itnitialValue);	
			ledPanel.add(led);
		}
		
		// Set MemoryImage into the table viewer
		//display.syncExec(new Runnable() {
		//	public void run() {
				viewer.setInput(ledPanel);
		//	}
		//});
	}
	
	private void createColumns(final TableViewer viewer, int numberOfColumn) {
		Table table = viewer.getTable();

		// Define Column
		for (int i = 0; i < numberOfColumn + 1; i++) {
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
			if(i%numberOfColumn ==0){
				column.getColumn().setWidth(0);
			}else {
				column.getColumn().setText(Integer.toString(i-1));
				column.getColumn().setResizable(false);
				column.getColumn().setMoveable(false);
				column.getColumn().setWidth(29);
				column.setEditingSupport(new LedEditingSupport(viewer, i-1));
			}
		}
			
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
	}
	
	public TableViewer createTableViewer(Composite parent, int numberOfColumn) {
		// Set ContentProvider
		TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);

		createColumns(viewer, numberOfColumn + 1);
		
		// Set ContentProvider
		viewer.setContentProvider(new LedContentProvider());
		
		// Set LabelProvider
		viewer.setLabelProvider(new LedLabelProvider(parent.getDisplay()));
		
		return viewer;
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
